package com.zr.web.controller.system;


import com.zr.common.annotation.Log;
import com.zr.common.core.controller.BaseController;
import com.zr.common.core.domain.AjaxResult;
import com.zr.common.core.domain.entity.SysUser;
import com.zr.common.enums.BusinessType;
import com.zr.common.utils.SecurityUtils;
import com.zr.common.utils.poi.ExcelUtil;
import com.zr.common.core.domain.entity.SysOrganization;
import com.zr.system.service.ISysOrganizationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 组织Controller
 * 
 * @author why
 * @date 2022-10-24
 */
@RestController
@RequestMapping("/system/organization")
public class SysOrganizationController extends BaseController
{
    @Resource
    private ISysOrganizationService sysOrganizationService;

    /**
     * 根据任务类型和登录人所在区域选择执行任务小组列表
     */
    @GetMapping("/listTaskGroup/{type}")
    public AjaxResult listTaskGroup(@PathVariable("type") String type)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        List<SysOrganization> list = sysOrganizationService.selectTaskGroupList(type,user.getOrganizationId());
        return AjaxResult.success(list);
    }

    /**
     * 查询组织列表
     */
    @PreAuthorize("@ss.hasPermi('system:organization:list')")
    @GetMapping("/list")
    public AjaxResult list(SysOrganization sysOrganization)
    {
        List<SysOrganization> list = sysOrganizationService.selectSysOrganizationList(sysOrganization);
        return AjaxResult.success(list);
    }

    /**
     * 导出组织列表
     */
    @PreAuthorize("@ss.hasPermi('system:organization:export')")
    @Log(title = "组织", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysOrganization sysOrganization)
    {
        List<SysOrganization> list = sysOrganizationService.selectSysOrganizationList(sysOrganization);
        ExcelUtil<SysOrganization> util = new ExcelUtil<SysOrganization>(SysOrganization.class);
        util.exportExcel(response, list, "组织数据");
    }

    /**
     * 获取组织详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:organization:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(sysOrganizationService.selectSysOrganizationById(id));
    }

    /**
     * 新增组织
     */
    @PreAuthorize("@ss.hasPermi('system:organization:add')")
    @Log(title = "组织", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysOrganization sysOrganization)
    {
        return toAjax(sysOrganizationService.insertSysOrganization(sysOrganization));
    }

    /**
     * 修改组织
     */
    @PreAuthorize("@ss.hasPermi('system:organization:edit')")
    @Log(title = "组织", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysOrganization sysOrganization)
    {
        return toAjax(sysOrganizationService.updateSysOrganization(sysOrganization));
    }

    /**
     * 删除组织
     */
    @PreAuthorize("@ss.hasPermi('system:organization:remove')")
    @Log(title = "组织", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysOrganizationService.deleteSysOrganizationByIds(ids));
    }
}
